var app = angular.module('statistics', []);
// 基于准备好的dom，初始化echarts实例
var barView = echarts.init(document.getElementById('barView'));

app.controller('listController', function ($scope,$http) {


    $scope.views = ['月视图','季视图','年视图'];
    $scope.pageIndex = '-1';
    $scope.viewChildList = getMonth();
    $scope.viewIndex = '月视图';
    $scope.childIndex = (new Date().getMonth()+1)+"月";//初始选中
    $scope.yearList = getYears();
    $scope.yearIndex = new Date().getFullYear()+"年";//初始年
    $scope.unChaneYear = true;//年视图隐藏第三栏
    $scope.hasDate = false;
    var nowYear =  new Date().getFullYear();
    var nowMonth = new Date().getMonth()+1;
    $scope.startDate = $scope.startDate ? $scope.startDate:nowYear+"-"+ setMonthNum(nowMonth)+"-01";
    $scope.endDate = $scope.endDate ? $scope.endDate : nowYear+"-"+ setMonthNum(nowMonth)+"-31 23:59:59";

    getData(-1);
    //初始化月份
    function getMonth(){
        var arr = [];
        for(var i = 1 ;i<=12;i++){
            arr.push(i +'月');
        }
        return arr;
    }
    //初始化季度
    function getNowQuarter(month,quarter){
        month = month?month:new Date().getMonth()+1;
        if((month>=1 && month <=3)||quarter =='第一季度')
            return {name :"第一季度",start:1,end:3};
        if((month>=4 && month <=6) ||quarter =='第二季度')
            return {name :"第二季度",start:4,end:6};
        if((month>=7 && month <=9) ||quarter =='第三季度')
            return {name :"第三季度",start:7,end:9};
        if((month>=10 && month <=12) ||quarter =='第四季度')
            return {name :"第四季度",start:10,end:12};
    }
    //初始化年份
    function getYears(){
        var year = new Date().getFullYear();
        var years = [];
        for(var i = year -5;i<=year;i++){
            years.push(i+"年")
        }
        return years;
    }
    //tab切换
    $scope.pageChange = function (pageType){
        $scope.pageIndex = pageType;
        getData(pageType);
    }
   
    //视图变更
    $scope.viewChange = function (view) {
        $scope.unChaneYear = true;
        $scope.viewChildList =  $scope.viewChildList.splice(0, $scope.viewChildList.length);
        if(view == "月视图"){
            $scope.viewChildList= getMonth();
            $scope.childIndex = (new Date().getMonth()+1)+"月";
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(new Date().getMonth()+1)+"-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(new Date().getMonth()+1)+"-31 23:59:59";
        }else if (view == '季视图'){
            $scope.viewChildList= ["第一季度","第二季度","第三季度","第四季度"];
            var res = getNowQuarter();
            $scope.childIndex =res.name;
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(res.start)+"-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(res.end)+"-31 23:59:59";
        }else{
            $scope.unChaneYear = false;
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+"01-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+"12-31 23:59:59";
        }

        $scope.pageChange($scope.pageIndex);
    }
    function setMonthNum(val){
        return val <10?"0"+val:val;
    }
    //查询变更
    $scope.chileChange = function (child) {
        if($scope.viewIndex == '月视图'){
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(child.replace("月",""))+"-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(child.replace("月",""))+"-31 23:59:59";

        }else if($scope.viewIndex == '季视图'){
            var res = getNowQuarter(10000,child);
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(res.start)+"-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+setMonthNum(res.end)+"-31 23:59:59";
        }else{
            $scope.startDate = $scope.yearIndex.replace("年","") +"-"+"01-01";
            $scope.endDate = $scope.yearIndex.replace("年","") +"-"+"12-31 23:59:59";
        }
        $scope.pageChange($scope.pageIndex);
    }
    //年份查询变更
    $scope.yearChange = function (child) {
        if($scope.viewIndex == '月视图'){
             $scope.startDate = child.replace("年","") +"-"+setMonthNum($scope.childIndex.replace("月",""))+"-01 00:00:00";
             $scope.endDate = child.replace("年","") +"-"+setMonthNum($scope.childIndex.replace("月",""))+"-31 23:59:59";
        }else if($scope.viewIndex == '季视图'){
            var res = getNowQuarter(10000,$scope.childIndex);
            $scope.startDate =child.replace("年","") +"-"+setMonthNum(res.start)+"-01";
            $scope.endDate = child.replace("年","") +"-"+setMonthNum(res.end)+"-31 23:59:59";
        }else{
            $scope.startDate = child.replace("年","") +"-"+"01-01";
            $scope.endDate = child.replace("年","") +"-"+"12-31 23:59:59";
        }
        $scope.pageChange($scope.pageIndex);
    }
    //获取数据
    function getData(type) {
        var nowYear =  new Date().getFullYear();
        var nowMonth = new Date().getMonth()+1;

        var url = "/smtool_project/wxapp/statistics/getMorningconferenceCount";
        if( type != -1)
            url = "/smtool_project/wxapp/statistics/getStockCount";
        console.log($scope.startDate);
        console.log($scope.endDate);
        $http.get(url,{
            params: {
                "startDate":$scope.startDate,
                "endDate":$scope.endDate,
                "type":type
            }
        }).then(function (result) {
            if(result && result.data.length > 0){
                barView.clear();
                if(type == -1)
                    initMorning(result);
                else
                    initRecommend(result);
            }else{
                $scope.hasDate = false;
            }

        });
    };
    function initMorning(result){
        var userList = []; var dataList = []; var comments = [];
        var legendDatas = ['评论', '发言'];
        var mornings = [];
        var res  =  result.data;
        for( i in res){
            userList.push(res[i].name);
            comments.push(res[i].comment?res[i].comment:0);
            mornings.push(res[i].morning);
        }
        dataList.splice(0,dataList.length);
        dataList.push({name:'评论',type:'bar',data:comments,itemStyle:{normal:{color:'#4ad2ff'}}});
        dataList.push({name:'发言',type:'bar',data:mornings,itemStyle:{normal:{color:'#FF3300'}}});
        $scope.hasDate = true;
        init(userList,dataList,legendDatas)
    }
    function initRecommend(result){
        var recommendList = []; var rateList = []; var sumRates = [];
        var legendDatas = ['收益率'];
        var res  =  result.data;
        for( i in res){
            recommendList.push(res[i].name);
            sumRates.push(res[i].sumRate?res[i].sumRate:0);
        }
        rateList.splice(0,rateList.length);
        rateList.push({name:'收益率',type:'bar',data:sumRates,itemStyle:{normal:{color:'#4ad2ff'}, label: { show: true,formatter: '{value}%'}  }});
        $scope.hasDate = true;
        init(recommendList,rateList,legendDatas,'{b}:\n{c}%')
    }
    //绘图
    function init(userList,dataList,legendDatas,formatter){

        option = {
            title: {
                text: '',
                subtext: ''
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                },
                formatter:formatter

            },
            legend: {
                data: legendDatas
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: {
                type: 'value',
                interval:1,
                boundaryGap: [0, 0]
            },
            yAxis: {
                type: 'category',
                data: userList
            },
            series: dataList
        };
        // 使用刚指定的配置项和数据显示图表。
        barView.setOption(option);

    }
});
